<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;


class Worker extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $needUserLogin = ['date'];


    /*
     *获取员工详情
     *
     *
     * */
    public function detail()
    {

        $worker_id = $this->request->request('id');

        if (!$worker_id) {
            $this->success('缺少必要参数！', null, 201);
        }

        $worker = Db::table('fa_worker')
            ->alias('a')
            ->field(['id', 'avatar', 'name', 'gender', 'age', 'score', 'service_count', 'a.createtime', 'introduce', 'experience', 'health_image', 'work_image', 'skill_images'])
            ->join('fa_worker_detail b', 'a.id=b.worker_id')
            ->where('id', $worker_id)
            ->find();

        $this->success('ok', ['worker' => $worker], 200);
    }

    /**
     *获取员工档期
     *
     *
     */
    public function date()
    {
        if ($this->request->post()) {

            $villageId = $this->request->post('village_id');
            $date = $this->request->post('date');
            $times = $this->request->post('times/a');

            $times = explode(",", $times[0]);

            if (!$villageId || !$date || !$times) {
                $this->success('缺少必要参数！', null, 201);
            }

            $workerList = Db::table('fa_village')
                ->field('fa_worker.id')
                ->join('fa_worker', 'fa_worker.group_id = fa_village.worker_group_id')
                ->where('fa_village.id', $villageId)
                ->where('fa_worker.deletetime', null)
                ->where('fa_village.deletetime', null)
                ->select();

            //组装data
            $data = ['times' => []];

            $tmpTime = [];

            foreach ($times as $time) {
                $tmpTime[$time] = false;
            }

            foreach ($workerList as $worker) {
                $data['times'][$worker['id']] = $tmpTime;
            }

            //查询每一条时间戳是否在工作档期 工作档期true  空闲false
            foreach ($data["times"] as $workerId => $times) {

                foreach ($times as $time => $exist) {

                    $exist = Db::table('fa_worker_calendar')
                        ->where($time . ' between starttime and endtime')
                        ->where('date', $date)
                        ->where('worker_id', $workerId)
                        ->find();
                    if ($exist) {
                        $data["times"][$workerId][$time] = true;
                    }

                }
            }

            $this->success('ok', $data, 200);
        }
    }

    public function getWorkerList()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        $poi_id = $this->request->get('poi_id');

        if (!$poi_id) {

            $this->success('缺少必要参数！', null, 201);

        }

        $village = Db::table('fa_village')->where('poi_id', $poi_id)->find();

        if (!$village) {//没有获取到小区
            $this->success('没有获取到小区！', ['workerList' => $village], 200);
        }

        $workerList = Db::table('fa_worker')
            ->field(['id', 'age', 'avatar', 'gender', 'name', 'score', 'service_count'])
            ->where('group_id', $village['worker_group_id'])
            ->where('deletetime', null)
            ->select();

        $this->success('ok', ['workerList' => $workerList], 200);
    }
}